# -*- coding: utf-8 -*-


import mod.server.extraServerApi as serverApi

from enchantBase import EnchantBase
from gemstoneScripts.config.sys import *

comp = serverApi.GetEngineCompFactory()

BLOCK_DAMAGE = ['contact', 'entity_attack', 'projectile', 'block_explosion', 'entity_explosion', 'fireworks',
                'freezing', 'stalactite', 'ram_attack', 'thorns', 'anvil', 'fire', 'fire_tick', 'lava']


class IceArmor(EnchantBase):
    def __init__(self, system, playerId, enchantData, levels):
        EnchantBase.__init__(self, system, playerId, enchantData, levels)
        self.timer = 0
        self.tick_timer = 0
        self.max_armor = 0
        self.armor = 0
        self.damage = 0

    def onEnter(self):
        self.mSystem.ListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', SET_PLAYER_ARMOR_EVENT, self,
                                    self.set_max_armor)
        self.mSystem.ListenForEvent(serverApi.GetEngineNamespace(), serverApi.GetEngineSystemName(),
                                    'DamageEvent', self, self.on_hit)
        self.try_open_hud()
        pass

    def onExit(self):
        self.mSystem.UnListenForEvent(MOD_NAME, MOD_LISTENER + 'EnchantsSystem', SET_PLAYER_ARMOR_EVENT, self,
                                      self.set_max_armor)
        self.mSystem.UnListenForEvent(serverApi.GetEngineNamespace(), serverApi.GetEngineSystemName(),
                                      'DamageEvent', self, self.on_hit)
        self.close_hud()
        pass

    def isArmorEnchant(self):
        return True

    def tick(self):
        if self.tick_timer >= 3:
            self.set_ice_armor()
            self.tick_timer = 0
        self.tick_timer += 1
        pass

    def second(self):
        self.get_player_armor()
        self.try_open_hud()
        self.timer += 1
        if self.timer >= 8:
            self.armor = self.max_armor
            self.timer = 0
        pass

    def on_hit(self, args):
        player_id = args['entityId']
        if player_id != self.playerId:
            return
        cause = args['cause']
        self.timer = 0
        dmg = args['damage']
        args['damage'] = 0
        if self.armor > 0 and cause in BLOCK_DAMAGE:
            args['knock'] = False
            if dmg <= self.armor:
                self.armor -= dmg
            else:
                self.armor = 0
            return
        src = args['srcId']
        projectile = args['projectileId']
        comp_attr = comp.CreateAttr(player_id)
        if dmg >= comp_attr.GetAttrValue(0):
            comp_game = comp.CreateGame(serverApi.GetLevelId())
            comp_game.KillEntity(player_id)
        comp_hurt = comp.CreateHurt(player_id)
        comp_hurt.Hurt(dmg, 'void', src, projectile, True)
        pass

    def try_open_hud(self):
        self.mSystem.NotifyToClient(self.playerId, OPEN_ICE_ARMOR_UI_EVNET, {'playerId': self.playerId})
        pass

    def close_hud(self):
        self.mSystem.NotifyToClient(self.playerId, CLOSE_ICE_ARMOR_UI_EVENT, {'playerId': self.playerId})
        pass

    def get_player_armor(self):
        self.mSystem.NotifyToClient(self.playerId, GET_PLAYER_ARMOR_EVENT, {'playerId': self.playerId})
        pass

    def set_ice_armor(self):
        self.mSystem.NotifyToClient(self.playerId, SET_ICE_ARMOR_EVENT, {'armor': self.armor})

    def set_max_armor(self, args):
        armor = args['armor']
        self.max_armor = int(sum(self.levels) * 0.5 * armor)
        pass
